Imports Microsoft.VisualBasic
Imports System.ComponentModel
Imports System.Configuration.Provider
Imports System.Web.Configuration


<DataObject(True)> _
Public Class MembershipsAndRolesODS

#Region "Initialized"

    Private Shared s_Initialized As Boolean
    Private Shared s_lock As New Object
    Private Shared s_Provider As MembershipsAndRolesProvider
    Private Shared s_Providers As MembershipsAndRolesProviderCollection

    Private Shared Sub Initialize()
        If Not s_Initialized Then
            'Avoid claiming lock if providers are already loaded
            If s_Provider Is Nothing Then
                SyncLock (s_lock)
                    'Get a reference to the <userInformation> section
                    Dim section As MembershipsAndRolesSection _
                        = CType(WebConfigurationManager.GetSection("DotNetPM.Business/membershipsAndRoles"), MembershipsAndRolesSection)
                    'Load registered providers and point _provider to the default provider
                    s_Providers = New MembershipsAndRolesProviderCollection()
                    ProvidersHelper.InstantiateProviders(section.Providers, s_Providers, GetType(MembershipsAndRolesProvider))
                    s_Provider = s_Providers(section.DefaultProvider)
                    If s_Provider Is Nothing Then Throw New ProviderException("Unable to load default UserInformationProvider")
                    s_Initialized = True
                End SyncLock
            End If
        End If
    End Sub

    Public Shared ReadOnly Property Provider() As MembershipsAndRolesProvider
        Get
            Initialize()
            Return s_Provider
        End Get
    End Property

#End Region

    Public Shared Function GetTitleOfCourtesy(ByVal index As Byte) As String
        ' Don't forget to correct anothoer GetTitleOfCourtesy function
        Select Case index
            Case 0 : Return ""
            Case 1 : Return "Mr."
            Case 2 : Return "Mrs."
            Case 3 : Return "Ms."
            Case 4 : Return "Dr."
            Case 5 : Return "Sr."
            Case Else : Return ""
        End Select
    End Function

    <DataObjectMethod(DataObjectMethodType.Select, False)> _
    Public Shared Function GetTitleOfCourtesy() As ICollection

        ' Create a table to store data for the DropDownList control.
        Dim dt As DataTable = New DataTable()

        ' Define the columns of the table.
        dt.Columns.Add(New DataColumn("TextField", GetType(String)))
        dt.Columns.Add(New DataColumn("ValueField", GetType(String)))

        ' Populate the table with sample values.
        ' Don't forget to correct anothoer GetTitleOfCourtesy function
        dt.Rows.Add(CreateRow("", "0", dt))
        dt.Rows.Add(CreateRow("Mr.", "1", dt))
        dt.Rows.Add(CreateRow("Mrs.", "2", dt))
        dt.Rows.Add(CreateRow("Ms.", "3", dt))
        dt.Rows.Add(CreateRow("Dr.", "4", dt))
        dt.Rows.Add(CreateRow("Sr.", "5", dt))

        ' Create a DataView from the DataTable to act as the data source
        ' for the DropDownList control.
        Dim dv As DataView = New DataView(dt)
        Return dv

    End Function

#Region "Private Subs and Functions"

    Private Shared Function CreateRow(ByVal Text As String, ByVal Value As String, ByVal dt As DataTable) As DataRow
        ' Create a DataRow using the DataTable defined in the 
        ' CreateDataSource method.
        Dim dr As DataRow = dt.NewRow()

        ' This DataRow contains the ColorTextField and ColorValueField 
        ' fields, as defined in the CreateDataSource method. Set the 
        ' fields with the appropriate value. Remember that column 0 
        ' is defined as ColorTextField, and column 1 is defined as 
        ' ColorValueField.
        dr(0) = Text
        dr(1) = Value
        Return dr
    End Function

#End Region


End Class
